package com.mlethe.library.navigation;

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.MenuRes;
import androidx.annotation.Px;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

/**
 * 默认样式导航栏
 *
 * @author Mlethe
 * @date 2018/2/1
 */

public class DefaultNavigationBar extends AbsNavigationBar<DefaultNavigationBar.Builder> {

    protected DefaultNavigationBar(Builder builder) {
        super(builder);
    }

    @Override
    public void attachNavigationParams() {
        super.attachNavigationParams();
        Builder builder = getBuilder();
        // 设置titleBar背景颜色
        Toolbar toolbar = getView(R.id.defaultToolbar);
        if (builder.overflowIcon != null) {
            toolbar.setOverflowIcon(builder.overflowIcon);
        }
        if (builder.iconColorFilter != null) {
            Drawable overflowIcon = toolbar.getOverflowIcon();
            if (overflowIcon != null) {
                overflowIcon.setColorFilter(builder.iconColorFilter);
            }
        }
        if (builder.menuId != 0) {
            toolbar.inflateMenu(builder.menuId);
        }
        toolbar.setOnMenuItemClickListener(builder.menuItemClickListener);
        if (builder.leftIcon != 0) {
            toolbar.setNavigationIcon(builder.leftIcon);
            toolbar.setNavigationOnClickListener(builder.leftClickListener);
        } else if (builder.leftDrawable != null) {
            toolbar.setNavigationIcon(builder.leftDrawable);
            toolbar.setNavigationOnClickListener(builder.leftClickListener);
        } else {
            toolbar.setNavigationOnClickListener(null);
        }
        // 设置标题
        TextView title = getView(R.id.toolbarTitleTv);
        if (builder.titleAtLeft) {
            toolbar.setTitle(title.getText());
            toolbar.setTitleTextColor(title.getCurrentTextColor());
            title.setVisibility(View.GONE);
        } else {
            if (builder.maxWidth != 0) {
                title.setMaxWidth(builder.maxWidth);
            }
            title.setCompoundDrawablePadding(builder.drawablePadding);
            title.setCompoundDrawablesWithIntrinsicBounds(builder.drawableLeft != 0 ? ContextCompat.getDrawable(builder.mContext, builder.drawableLeft) : null,
                    builder.drawableTop != 0 ? ContextCompat.getDrawable(builder.mContext, builder.drawableTop) : null,
                    builder.drawableRight != 0 ? ContextCompat.getDrawable(builder.mContext, builder.drawableRight) : null,
                    builder.drawableBottom != 0 ? ContextCompat.getDrawable(builder.mContext, builder.drawableBottom) : null);
        }
        // 设置右边图片
        ImageView rightIcon = getView(R.id.toolbarRightIv);
        if (builder.isSetIconMargin) {
            Toolbar.MarginLayoutParams layoutParams = (Toolbar.MarginLayoutParams) rightIcon.getLayoutParams();
            layoutParams.leftMargin = builder.rightIconMarginLeft;
            layoutParams.topMargin = builder.rightIconMarginTop;
            layoutParams.rightMargin = builder.rightIconMarginRight;
            layoutParams.bottomMargin = builder.rightIconMarginBottom;
            rightIcon.setLayoutParams(layoutParams);
        }
        if (builder.rightIconScaleType != null) {
            rightIcon.setScaleType(builder.rightIconScaleType);
        }
        // 设置右边文字
        TextView rightText = getView(R.id.toolbarRightTv);
        if (builder.isSetTextMargin) {
            Toolbar.MarginLayoutParams layoutParams = (Toolbar.MarginLayoutParams) rightText.getLayoutParams();
            layoutParams.leftMargin = builder.rightTextMarginLeft;
            layoutParams.topMargin = builder.rightTextMarginTop;
            layoutParams.rightMargin = builder.rightTextMarginRight;
            layoutParams.bottomMargin = builder.rightTextMarginBottom;
            rightText.setLayoutParams(layoutParams);
        }
        rightText.setCompoundDrawablePadding(builder.rightDrawablePadding);
        rightText.setCompoundDrawablesWithIntrinsicBounds(builder.rightDrawableLeft != 0 ? ContextCompat.getDrawable(builder.mContext, builder.rightDrawableLeft) : null,
                builder.rightDrawableTop != 0 ? ContextCompat.getDrawable(builder.mContext, builder.rightDrawableTop) : null,
                builder.rightDrawableRight != 0 ? ContextCompat.getDrawable(builder.mContext, builder.rightDrawableRight) : null,
                builder.rightDrawableBottom != 0 ? ContextCompat.getDrawable(builder.mContext, builder.rightDrawableBottom) : null);
    }

    /**
     * 获取导航栏
     *
     * @return
     */
    public Toolbar getToolbar() {
        return getView(R.id.defaultToolbar);
    }

    /**
     * 获取标题View
     *
     * @return
     */
    public TextView getTitleView() {
        return getView(R.id.toolbarTitleTv);
    }

    /**
     * 获取右边文字View
     *
     * @return
     */
    public TextView getRightTextView() {
        return getView(R.id.toolbarRightTv);
    }

    /**
     * 获取右边图片View
     *
     * @return
     */
    public ImageView getRightImageView() {
        return getView(R.id.toolbarRightIv);
    }


    /**
     * 导航栏的Builder
     */
    public static class Builder extends AbsNavigationBar.Builder<Builder> {

        // 2.所有的效果放置
        private Drawable overflowIcon;
        private ColorFilter iconColorFilter;
        @MenuRes
        private int menuId = 0;
        private Toolbar.OnMenuItemClickListener menuItemClickListener;
        @DrawableRes
        private int leftIcon = 0;
        private Drawable leftDrawable;

        /**
         * 标题在左边 true 是   false 否
         */
        private boolean titleAtLeft = false;
        private int maxWidth = 0;

        @DrawableRes
        private int drawableLeft, drawableTop, drawableRight, drawableBottom;

        private int drawablePadding = 0;

        private ImageView.ScaleType rightIconScaleType;

        private boolean isSetIconMargin;

        private int rightIconMarginLeft, rightIconMarginTop, rightIconMarginRight, rightIconMarginBottom;

        private boolean isSetTextMargin;

        private int rightTextMarginLeft, rightTextMarginTop, rightTextMarginRight, rightTextMarginBottom;

        @DrawableRes
        private int rightDrawableLeft, rightDrawableTop, rightDrawableRight, rightDrawableBottom;

        private int rightDrawablePadding = 0;

        public Builder(ViewGroup parent) {
            super(parent, R.layout.default_title_bar);
        }

        public Builder(Context context) {
            super(context, R.layout.default_title_bar);
        }

        @Override
        public DefaultNavigationBar create() {
            return new DefaultNavigationBar(this);
        }

        @Override
        public void detached() {
            super.detached();
            leftDrawable = null;
            overflowIcon = null;
            iconColorFilter = null;
            rightIconScaleType = null;
            menuItemClickListener = null;
        }

        /**
         * 设置三个点的更多按钮图标
         *
         * @param resId
         */
        public Builder setOverflowIcon(@DrawableRes int resId) {
            return setOverflowIcon(ContextCompat.getDrawable(mContext, resId));
        }

        /**
         * 设置三个点的更多按钮图标
         *
         * @param overflowIcon
         */
        public Builder setOverflowIcon(Drawable overflowIcon) {
            this.overflowIcon = overflowIcon;
            return this;
        }

        /**
         * 设置三个点的更多图标的颜色
         *
         * @param iconColorFilter
         * @return
         */
        public Builder setOverflowIconColor(ColorFilter iconColorFilter) {
            this.iconColorFilter = iconColorFilter;
            return this;
        }

        /**
         * 设置三个点的更多图标的颜色
         *
         * @param color
         * @return
         */
        public Builder setOverflowIconColorInt(@ColorInt int color) {
            return setOverflowIconColor(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
        }

        /**
         * 设置三个点的更多图标的颜色
         *
         * @param colorRes
         * @return
         */
        public Builder setOverflowIconColor(@ColorRes int colorRes) {
            return setOverflowIconColor(new PorterDuffColorFilter(ContextCompat.getColor(mContext, colorRes), PorterDuff.Mode.SRC_IN));
        }

        /**
         * 设置三个点的更多图标的颜色
         *
         * @param colorString
         * @return
         */
        public Builder setOverflowIconColor(String colorString) {
            return setOverflowIconColor(new PorterDuffColorFilter(Color.parseColor(colorString), PorterDuff.Mode.SRC_IN));
        }

        /**
         * 设置menu资源文件
         *
         * @param menuId
         */
        public Builder setMenuId(@MenuRes int menuId) {
            this.menuId = menuId;
            return this;
        }

        /**
         * 设置menu点击事件
         *
         * @param menuItemClickListener
         * @return
         */
        public Builder setOnMenuItemClickListener(Toolbar.OnMenuItemClickListener menuItemClickListener) {
            this.menuItemClickListener = menuItemClickListener;
            return this;
        }

        /**
         * 隐藏左边图片
         *
         * @return
         */
        public Builder hideLeftIcon() {
            this.leftIcon = 0;
            this.leftDrawable = null;
            return this;
        }

        /**
         * 设置左边图片
         *
         * @param leftRes
         * @return
         */
        public Builder setLeftIcon(@DrawableRes int leftRes) {
            this.leftIcon = leftRes;
            return this;
        }

        /**
         * 设置左边图片
         *
         * @param drawable drawable
         * @return
         */
        public Builder setLeftIcon(Drawable drawable) {
            this.leftDrawable = drawable;
            return this;
        }

        /**
         * 设置左边点击事件
         *
         * @param clickListener
         * @return
         */
        public Builder setLeftClickListener(View.OnClickListener clickListener) {
            this.leftClickListener = clickListener;
            return this;
        }

        /**
         * 标题靠左
         *
         * @return
         */
        public Builder setTitleAtLeft() {
            this.titleAtLeft = true;
            return this;
        }

        /**
         * 设置title
         *
         * @param text
         * @return
         */
        public Builder setTitle(CharSequence text) {
            return setText(R.id.toolbarTitleTv, text);
        }

        /**
         * 设置title
         *
         * @param resid
         * @return
         */
        public Builder setTitle(@StringRes int resid) {
            return setText(R.id.toolbarTitleTv, resid);
        }

        /**
         * 设置title
         *
         * @param value
         * @return
         */
        public Builder setTitleInt(int value) {
            return setTitle(String.valueOf(value));
        }

        /**
         * 设置title
         *
         * @param value
         * @return
         */
        public Builder setTitle(float value) {
            return setTitle(String.valueOf(value));
        }

        /**
         * 设置title
         *
         * @param value
         * @return
         */
        public Builder setTitle(double value) {
            return setTitle(String.valueOf(value));
        }

        /**
         * 设置title
         *
         * @param value
         * @return
         */
        public Builder setTitle(long value) {
            return setTitle(String.valueOf(value));
        }

        /**
         * 设置标题文字颜色
         *
         * @param color
         * @return
         */
        public Builder setTitleColorInt(@ColorInt int color) {
            return setTextColorInt(R.id.toolbarTitleTv, color);
        }

        /**
         * 设置标题文字颜色
         *
         * @param colorRes
         * @return
         */
        public Builder setTitleColor(@ColorRes int colorRes) {
            return setTextColor(R.id.toolbarTitleTv, colorRes);
        }

        /**
         * 设置标题文字颜色
         *
         * @param colorString
         * @return
         */
        public Builder setTitleColor(String colorString) {
            return setTextColor(R.id.toolbarTitleTv, colorString);
        }

        /**
         * 设置标题文字大小
         *
         * @param textSize sp
         * @return
         */
        public Builder setTitleSize(float textSize) {
            return setTextSize(R.id.toolbarTitleTv, textSize);
        }

        /**
         * 设置标题最大宽度
         *
         * @param maxWidth px
         * @return
         */
        public Builder setTitleMaxWidth(@Px int maxWidth) {
            this.maxWidth = maxWidth;
            return this;
        }

        /**
         * 设置标题最大宽度
         *
         * @param maxWidth dp
         * @return
         */
        public Builder setTitleMaxWidth(float maxWidth) {
            return setTitleMaxWidth((int) dp2px(maxWidth));
        }

        /**
         * 设置标题点击事件
         *
         * @param titleClickListener
         * @return
         */
        public Builder setTitleClickListener(View.OnClickListener titleClickListener) {
            return setOnClickListener(R.id.toolbarTitleTv, titleClickListener);
        }

        /**
         * 设置标题图片和文字之间的间距
         *
         * @param drawablePadding px
         * @return
         */
        public Builder setDrawablePadding(@Px int drawablePadding) {
            this.drawablePadding = drawablePadding;
            return this;
        }

        /**
         * 设置标题图片和文字之间的间距
         *
         * @param drawablePadding dp
         * @return
         */
        public Builder setDrawablePadding(float drawablePadding) {
            return setDrawablePadding((int) dp2px(drawablePadding));
        }

        /**
         * 设置标题图片
         *
         * @param left
         * @param top
         * @param right
         * @param bottom
         * @return
         */
        public Builder setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
            this.drawableLeft = left;
            this.drawableTop = top;
            this.drawableRight = right;
            this.drawableBottom = bottom;
            return this;
        }

        /**
         * 设置右边图片的宽度
         *
         * @param width px
         * @return
         */
        public Builder setRightIconWidth(@Px int width) {
            return setWidth(R.id.toolbarRightIv, width);
        }

        /**
         * 设置右边图片的宽度
         *
         * @param width dp
         * @return
         */
        public Builder setRightIconWidth(float width) {
            return setWidth(R.id.toolbarRightIv, width);
        }

        /**
         * 设置右边的图片
         *
         * @param rightRes
         * @return
         */
        public Builder setRightIcon(@DrawableRes int rightRes) {
            return setIcon(R.id.toolbarRightIv, rightRes);
        }

        /**
         * 设置右边的图片显示模式
         *
         * @param scaleType
         */
        public Builder setRightIconScaleType(ImageView.ScaleType scaleType) {
            this.rightIconScaleType = scaleType;
            return this;
        }

        /**
         * 设置右边图片显示
         *
         * @param visibility
         * @return
         */
        public Builder setRightIconVisibility(int visibility) {
            return setVisibility(R.id.toolbarRightIv, visibility);
        }

        /**
         * 设置右边图片padding
         *
         * @param padding px
         * @return
         */
        public Builder setRightIconPadding(@Px int padding) {
            return setPadding(R.id.toolbarRightIv, padding);
        }

        /**
         * 设置右边图片padding
         *
         * @param padding dp
         * @return
         */
        public Builder setRightIconPadding(float padding) {
            return setPadding(R.id.toolbarRightIv, padding);
        }

        /**
         * 设置右边图片padding
         *
         * @param left   px
         * @param top    px
         * @param right  px
         * @param bottom px
         * @return
         */
        public Builder setRightIconPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            return setPadding(R.id.toolbarRightIv, left, top, right, bottom);
        }

        /**
         * 设置右边图片padding
         *
         * @param left   dp
         * @param top    dp
         * @param right  dp
         * @param bottom dp
         * @return
         */
        public Builder setRightIconPadding(float left, float top, float right, float bottom) {
            return setPadding(R.id.toolbarRightIv, left, top, right, bottom);
        }

        /**
         * 设置右边图片margin
         *
         * @param margin px
         * @return
         */
        public Builder setRightIconMargin(@Px int margin) {
            return setRightIconMargin(margin, margin, margin, margin);
        }

        /**
         * 设置右边图片margin
         *
         * @param margin
         * @return
         */
        public Builder setRightIconMargin(float margin) {
            return setRightIconMargin(margin, margin, margin, margin);
        }

        /**
         * 设置右边图片margin
         *
         * @param left
         * @param top
         * @param right
         * @param bottom
         * @return
         */
        public Builder setRightIconMargin(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            isSetIconMargin = true;
            this.rightIconMarginLeft = left;
            this.rightIconMarginTop = top;
            this.rightIconMarginRight = right;
            this.rightIconMarginBottom = bottom;
            return this;
        }

        /**
         * 设置右边图片margin
         *
         * @param left   dp
         * @param top    dp
         * @param right  dp
         * @param bottom dp
         * @return
         */
        public Builder setRightIconMargin(float left, float top, float right, float bottom) {
            return setRightIconMargin((int) dp2px(left), (int) dp2px(top), (int) dp2px(right), (int) dp2px(bottom));
        }

        /**
         * 设置右边图片点击事件
         *
         * @param clickListener
         * @return
         */
        public Builder setRightIconClickListener(View.OnClickListener clickListener) {
            return setOnClickListener(R.id.toolbarRightIv, clickListener);
        }

        /**
         * 设置右边文字的宽度
         *
         * @param width
         * @return
         */
        public Builder setRightTextWidth(@Px int width) {
            return setWidth(R.id.toolbarRightTv, width);
        }

        /**
         * 设置右边文本
         *
         * @param rightText
         * @return
         */
        public Builder setRightText(CharSequence rightText) {
            return setText(R.id.toolbarRightTv, rightText);
        }

        /**
         * 设置右边文本
         *
         * @param resid
         * @return
         */
        public Builder setRightText(@StringRes int resid) {
            return setText(R.id.toolbarRightTv, resid);
        }

        /**
         * 设置右边文本
         *
         * @param value
         * @return
         */
        public Builder setRightTextInt(int value) {
            return setRightText(String.valueOf(value));
        }

        /**
         * 设置右边文本
         *
         * @param value
         * @return
         */
        public Builder setRightText(float value) {
            return setRightText(String.valueOf(value));
        }

        /**
         * 设置右边文本
         *
         * @param value
         * @return
         */
        public Builder setRightText(double value) {
            return setRightText(String.valueOf(value));
        }

        /**
         * 设置右边文本
         *
         * @param value
         * @return
         */
        public Builder setRightText(long value) {
            return setRightText(String.valueOf(value));
        }

        /**
         * 设置右边文字大小
         *
         * @param textSize sp
         * @return
         */
        public Builder setRightTextSize(float textSize) {
            return setTextSize(R.id.toolbarRightTv, textSize);
        }

        /**
         * 设置右边文字颜色
         *
         * @param color
         * @return
         */
        public Builder setRightTextColorInt(@ColorInt int color) {
            return setTextColorInt(R.id.toolbarRightTv, color);
        }

        /**
         * 设置右边文字颜色
         *
         * @param colorRes
         * @return
         */
        public Builder setRightTextColor(@ColorRes int colorRes) {
            return setRightTextColorInt(ContextCompat.getColor(mContext, colorRes));
        }

        /**
         * 设置右边文字颜色
         *
         * @param colorString
         * @return
         */
        public Builder setRightTextColor(String colorString) {
            return setRightTextColorInt(Color.parseColor(colorString));
        }

        /**
         * 设置右边文字显示
         *
         * @param visibility
         * @return
         */
        public Builder setRightTextVisibility(int visibility) {
            return setVisibility(R.id.toolbarRightTv, visibility);
        }

        /**
         * 设置右边文字padding
         *
         * @param padding px
         * @return
         */
        public Builder setRightTextPadding(@Px int padding) {
            return setPadding(R.id.toolbarRightTv, padding);
        }

        /**
         * 设置右边文字padding
         *
         * @param padding dp
         * @return
         */
        public Builder setRightTextPadding(float padding) {
            return setPadding(R.id.toolbarRightTv, padding);
        }

        /**
         * 设置右边文字padding
         *
         * @param left   px
         * @param top    px
         * @param right  px
         * @param bottom px
         * @return
         */
        public Builder setRightTextPadding(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            return setPadding(R.id.toolbarRightTv, left, top, right, bottom);
        }

        /**
         * 设置右边文字padding
         *
         * @param left   dp
         * @param top    dp
         * @param right  dp
         * @param bottom dp
         * @return
         */
        public Builder setRightTextPadding(float left, float top, float right, float bottom) {
            return setPadding(R.id.toolbarRightTv, left, top, right, bottom);
        }

        /**
         * 设置右边文字margin
         *
         * @param margin px
         * @return
         */
        public Builder setRightTextMargin(@Px int margin) {
            return setRightTextMargin(margin, margin, margin, margin);
        }

        /**
         * 设置右边文字margin
         *
         * @param margin dp
         * @return
         */
        public Builder setRightTextMargin(float margin) {
            return setRightTextMargin(margin, margin, margin, margin);
        }

        /**
         * 设置右边文字margin
         *
         * @param left   px
         * @param top    px
         * @param right  px
         * @param bottom px
         * @return
         */
        public Builder setRightTextMargin(@Px int left, @Px int top, @Px int right, @Px int bottom) {
            isSetTextMargin = true;
            this.rightTextMarginLeft = left;
            this.rightTextMarginTop = top;
            this.rightTextMarginRight = right;
            this.rightTextMarginBottom = bottom;
            return this;
        }

        /**
         * 设置右边文字margin
         *
         * @param left   dp
         * @param top    dp
         * @param right  dp
         * @param bottom dp
         * @return
         */
        public Builder setRightTextMargin(float left, float top, float right, float bottom) {
            return setRightTextMargin((int) dp2px(left), (int) dp2px(top), (int) dp2px(right), (int) dp2px(bottom));
        }

        /**
         * 右边文字设置图片
         *
         * @param left
         * @param top
         * @param right
         * @param bottom
         * @return
         */
        public Builder setRightCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
            this.rightDrawableLeft = left;
            this.rightDrawableTop = top;
            this.rightDrawableRight = right;
            this.rightDrawableBottom = bottom;
            return this;
        }

        /**
         * 设置右边图片和文字之间的间距
         *
         * @param drawablePadding px
         * @return
         */
        public Builder setRightDrawablePadding(@Px int drawablePadding) {
            this.rightDrawablePadding = drawablePadding;
            return this;
        }

        /**
         * 设置右边图片和文字之间的间距
         *
         * @param drawablePadding dp
         * @return
         */
        public Builder setRightDrawablePadding(float drawablePadding) {
            return setRightDrawablePadding((int) dp2px(drawablePadding));
        }

        /**
         * 设置右边文字点击事件
         *
         * @param clickListener
         * @return
         */
        public Builder setRightTextClickListener(View.OnClickListener clickListener) {
            return setOnClickListener(R.id.toolbarRightTv, clickListener);
        }

    }
}
